package com.itboye.textileexpres.order;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.MyOrderBeans;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.MainApplication;
import com.itboye.textileexpres.base.WebActivity;
import com.itboye.textileexpres.util.ClickListener;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.PriceChange;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;
/***
 * 待收货
 * @author itboye
 *
 */
public class WanChengGoodAdapter extends BaseAdapter implements OnClickListener {
	Context context;
	MyOrderBeans beans;
	ClickListener listOnclik;

	public WanChengGoodAdapter(Context context, MyOrderBeans beans,
			ClickListener listOnclik) {
		this.context = context;
		this.beans = beans;
		this.listOnclik = listOnclik;
	}

	@Override
	public int getCount() {
		return beans.getList().size();
	}

	@Override
	public Object getItem(int position) {
		
		return position;
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		String string = beans.getList().get(position).getStatus();
		int item = Integer.parseInt(string);

		return item;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		viewHolder holder = null;
		if (convertView == null) {
			holder = new viewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(
					R.layout.item_order_wancheng, null);
			holder.area_tv = (TextView) convertView.findViewById(R.id.area_tv);
			holder.state_tv = (TextView) convertView.findViewById(R.id.state_tv);
			holder.publish_author_tv = (TextView) convertView.findViewById(R.id.publish_author_tv);
			holder.chakanwuliu = (TextView) convertView.findViewById(R.id.chakanwuliu);
			holder.linwancheng=(LinearLayout) convertView.findViewById(R.id.linwancheng);
			convertView.setTag(holder);
		} else {
			holder = (viewHolder) convertView.getTag();
		}
		holder.area_tv.setText("产地:" + beans.getList().get(position).getNote());
		
		
		holder.publish_author_tv.setText("发布人:"+ beans.getList().get(position).getPublisher_name());
		holder.chakanwuliu.setOnClickListener(this);
		holder.chakanwuliu.setTag(position);
		holder.state_tv.setText("交易成功");
		
		holder.linwancheng.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Intent intent;
				intent = new Intent(context, OrderDetailActivityJiaoyiwancheng.class);
				intent.putExtra("order_code", beans.getList().get(position)
						.getOrder_code());
				intent.putExtra("str", beans.getList().get(position)
						.getQuery_status());
				context.startActivity(intent);
			}
		});

		/**
		 * 第二级
		 */

		int lenth = beans.getList().get(position).getItems().size();
		holder.linwancheng.removeAllViewsInLayout();
		for (int i = 0; i < lenth; i++) {
			//获取控件
			View view=LayoutInflater.from(context).inflate(R.layout.item_order_add, null);
			TextView goods_name1_tv = (TextView) view.findViewById(R.id.goods_name1_tv);
			TextView goods_name2_tv = (TextView) view.findViewById(R.id.goods_name2_tv);
			TextView goods_detail_tv = (TextView) view.findViewById(R.id.goods_detail_tv);

			TextView color_tv = (TextView) view.findViewById(R.id.color_tv);
			TextView price_tv = (TextView) view.findViewById(R.id.price_tv);
			TextView num_tv = (TextView) view.findViewById(R.id.num_tv);
			TextView total_num_tv = (TextView) view.findViewById(R.id.total_num_tv);

			TextView total_price_tv = (TextView) view.findViewById(R.id.total_price_tv);
			TextView freight_tv = (TextView) view.findViewById(R.id.freight_tv);
//			ImageView goods_img = (ImageView) convertView.findViewById(R.id.goods_img);
			ImageView img = (ImageView) view.findViewById(R.id.goods_img);
			//加载数据 15258861285
			goods_name1_tv.setText(beans.getList().get(position).getItems().get(i).getOrder_code());
			goods_name2_tv.setText(beans.getList().get(position).getItems().get(i).getName());  
			goods_detail_tv.setText(beans.getList().get(position).getItems().get(i).getName());

			color_tv.setText(beans.getList().get(position).getItems().get(i).getSku_desc());
			price_tv.setText(beans.getList().get(position).getItems().get(i).getOri_price());
			num_tv.setText("X"+beans.getList().get(position).getItems().get(i).getCount());

			total_num_tv.setText("共"+ beans.getList().get(position).getItems().get(i).getDt_goods_unit() + "米");
			total_price_tv.setText("合计"+PriceChange.Change( beans.getList().get(position).getItems().get(i).getPrice())+ "元");
			goods_detail_tv.setText(beans.getList().get(position).getItems().get(i).getDt_goods_unit());
			freight_tv.setText("(包含运费"+beans.getList().get(position).getItems().get(i).getWeight()+"元)");
			
			XImageLoader.load(
					GlobalConfig.IMAGE_DOUBLE
							+ beans.getList().get(position).getItems().get(i)
									.getImg(), img);
			MarginLayoutParams layoutParams = new MarginLayoutParams(
					MarginLayoutParams.WRAP_CONTENT,
					MarginLayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(5, 5, 5, 5);
	
			holder.linwancheng.addView(view, layoutParams);
			
		}
		
		return convertView;

	}

	/**
	 * 查看物流
	 * 
	 * @param v
	 */
	public void onAddWuLiu(int positon, View v) {
		Intent intent = new Intent(MainApplication.context, WebActivity.class);
		intent.putExtra("url",
				"http://api.guannan.itboye.com/public/web.php/Express/index?order_code="
						+ beans.getList().get(positon).getOrder_code() + "39"
						+ IsMobileNumPhone.isPasswrd());
		intent.putExtra("title", "物流查看");
		context.startActivity(intent);
	}

	/**
	 * 确认收货
	 * 
	 * @author itboye
	 * 
	 */

	class viewHolder {
		private TextView area_tv;
		private TextView state_tv;
		private TextView publish_author_tv;
		private TextView  chakanwuliu;
		private LinearLayout linwancheng;

	}

	

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		
		case R.id.chakanwuliu:
			listOnclik.onWuliu(v);
			break;
		

		default:
			break;
		}
	}

}
